package com.lsy.weizhi2.popupwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.myutils.AddressCall;
import com.lsy.weizhi2.utils.TextHelper;
import com.lsy.weizhi2.wheel.ArrayWheelAdapter;
import com.lsy.weizhi2.wheel.OnWheelChangedListener;
import com.lsy.weizhi2.wheel.WheelView;


/**
 * Created by 嗨哟 on 2017/1/11.
 */
public class SelectPopup implements View.OnClickListener,OnWheelChangedListener {
    private View view;
    private WheelView dialog_first,dialog_second,dialog_third;
    private TextView dialog_quxiao,dialog_queding;
    private Context context;
    private String[] strs;
    private int showCount;
    private String flag;
    private String first,second,third;
    private static PopupWindow popupWindow;
    private AddressCall call;

    public SelectPopup(AddressCall call) {
        this.call = call;
    }

    public void setShowCount(int count){
        showCount=count;
    }
    public void setStrs(String[] strs){
        this.strs=strs;
    }
    public void setFlag(String flag){this.flag=flag;}
    public void init(Context context){
        View view=View.inflate(context, R.layout.selectdialog,null);
        this.context=context;
        dialog_quxiao= (TextView) view.findViewById(R.id.dialog_quxiao);
        dialog_queding= (TextView) view.findViewById(R.id.dialog_queding);
        dialog_first= (WheelView) view.findViewById(R.id.dialog_first);
        dialog_second= (WheelView) view.findViewById(R.id.dialog_second);
        dialog_third= (WheelView) view.findViewById(R.id.dialog_third);
        dialog_first.addChangingListener(this);
        dialog_second.addChangingListener(this);
        dialog_third.addChangingListener(this);
        dialog_quxiao.setOnClickListener(this);
        dialog_queding.setOnClickListener(this);
        if(showCount==1){
            dialog_second.setVisibility(View.GONE);
            dialog_third.setVisibility(View.GONE);
            dialog_first.setViewAdapter(new ArrayWheelAdapter<String>(context, strs));
            dialog_first.setCyclic(true);
            dialog_first.setCurrentItem(0);
            first=strs[0];
        }else if(showCount==2){
            dialog_second.setVisibility(View.GONE);
            dialog_first.setViewAdapter(new ArrayWheelAdapter<String>(context, TextHelper.time));
            dialog_first.setCyclic(true);
            dialog_first.setCurrentItem(0);
            first=TextHelper.time[0];
            dialog_third.setViewAdapter(new ArrayWheelAdapter<String>(context,TextHelper.date));
            dialog_third.setCyclic(true);
            dialog_third.setCurrentItem(0);
            third=TextHelper.date[0];
        }else if(showCount==3){//地址选择
            dialog_first.setViewAdapter(new ArrayWheelAdapter<String>(context, TextHelper.province));
            dialog_first.setCyclic(true);
            dialog_first.setCurrentItem(0);
            third=TextHelper.province[0];
            updatesecond();
        }
        popupWindow=new PopupWindow(context);
        popupWindow.setContentView(view);
        popupWindow.setWidth(BaseApp.SCREEN_WIDTH);
        popupWindow.setHeight(BaseApp.SCREEN_HEIGHT*1/4);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.select_popup);
        popupWindow.showAsDropDown(view,0,BaseApp.SCREEN_HEIGHT*3/4);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_quxiao:
                popupWindow.dismiss();
                break;
            case R.id.dialog_queding:
                //MyMethod.showToast(context,first+second+third);
                call.Select(first,second,third,flag);
                popupWindow.dismiss();
                break;
        }
    }
    private void updatesecond(){
        int pCurrent = dialog_first.getCurrentItem();
        first = TextHelper.province[pCurrent];
        String[] cities = TextHelper.citys.get(first);
        if (cities == null) {
            cities = new String[]{""};
        }
        dialog_second.setViewAdapter(new ArrayWheelAdapter<String>(context, cities));
        dialog_second.setCurrentItem(0);
        updatethird();
    }
    private void updatethird(){
        int cCurrent = dialog_second.getCurrentItem();
        second = TextHelper.citys.get(first)[cCurrent];
        String[] areas = TextHelper.district.get(second);
        if (areas == null) {
            areas = new String[]{""};
        }
        dialog_third.setViewAdapter(new ArrayWheelAdapter<String>(context, areas));
        dialog_third.setCurrentItem(0);
        third = areas[0];
    }
    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if(wheel==dialog_first){
            if(showCount==3){
                updatesecond();
            }else if(showCount==2){
                first=TextHelper.time[newValue];
            }else{
                first=strs[newValue];
            }
        }else if(wheel==dialog_second){
            if(showCount==3){
                updatethird();
            }else{

            }

        }else if(wheel==dialog_third){
            if(showCount==3){
                third = TextHelper.district.get(second)[newValue];
            }else{
                third=TextHelper.date[newValue];
            }

        }
    }
}
